import React from 'react'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import Table from '#cpt/temp/Table'
import TableControls from '#cpt/temp/TableControls'
import Controls from '#cpt/temp/Controls'
import Confirm from '#cpt/temp/Confirm'
// ===================================================================== global declare
const { $fn, $Lazy } = window
// ===================================================================== Lazy
const Page 				= $Lazy.load(()=>import('#tp/page/page-container'))
const $cache 			= $Lazy.file(()=>import('#private/cache-api'))

const enabledData		= [{ name:'未审核', value: '0' },{ name:'审核通过', value: '1' },{ name:'审核未通过', value: '-1' }]
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		selectedKeys:[],
		selectedRows:[],
		// 搜索表单列表
		forms : [
			{ label: '医院名称',		name: 'hosp_id', 		type: 'select',nameStr:'name' },
			{ label: '医院性质',		name: 'nature',			type: 'select', loading:true,	idStr:'code' },
			{ label: '医院代码',		name: 'hosp_code' },
			{ label: '医院条码',		name: 'hosp_spec' },
			{ label: '医院级别',		name: 'level',			type: 'select', loading:true,	idStr:'code' },
			{ label: '状态',			name: 'enabled',		type: 'select', data:enabledData },
			{ label: '区域',			name: 'region_id',		type: 'select', loading:true },
			{ label: '业务员',		name: 'salesman_id',	type: 'select', loading:true },
		]
	}
	
	model = {}
	query = { enabled:'' }
	
	componentDidMount(){
		this.getSelect()
		if(window.isInit){
			this.fetch()
		}
		$fn.fresh.on(()=>{
			this.fetch()
			this.getHosptal()
		})
	}
	componentWillUnmount(){
		$fn.fresh.remove()
	}
	
	getHosptal = () => {
		const { forms } = this.state
		$cache.then( async f => {
			// 医院名称
			$fn.setRow(forms, 'hosp_id', await f.getCache({ api:'bind_hospital' } ) )	 // 取消获取全部医院(未审核) 2021-8-2 --马婷 
			//
			this.setState({ forms })
		})
	}
	// 获取下拉数据
	getSelect = () => {
		const { forms } = this.state
		$cache.then( async f => {
			// 医院名称 hospital
			$fn.setRow(forms, 'hosp_id', await f.getCache({ api:'bind_hospital' } ) ) // 医院api 2022年11月11日16:00:43 敖得卓
			// 区域
			$fn.setRow(forms, 'region_id', await f.getCache({ api:'bs_area' } ) )
			//医院性质
			$fn.setRow(forms, 'nature', await f.getDicCache({ code:44100 } ) )
			//医院级别
			$fn.setRow(forms, 'level', await f.getDicCache({ code:39000 } ) )
			// 业务员
			$fn.setRow(forms, 'salesman_id', await f.getCache({ api:'salesman' } ) )
			//
			this.setState({ forms })
		})
	}
	
	// paging
	fetch = () => $fn.fetch.call(this,'bs-hospital/index')
	
	// 表单
	cols = [
		{ title: '医院代码',		dataIndex: 'hosp_code', 	...$fn.table.hosp_code },
		{ title: '医院条码', 	dataIndex: 'hosp_spec', 	...$fn.table.num },
		{ title: '医院名称', 	dataIndex: 'hosp_name', 	width:200 },
		{ title: '医院性质', 	dataIndex: 'nature_name', 	width:100 },
		{ title: '医院级别',		dataIndex: 'level_name', 	width:100 },
		{ title: 'NC单位',		dataIndex: 'nc_name', 		width:180 },
		{ title: 'NC单位编码',	dataIndex: 'organization_code', 	width:150 },
		{ title: '开票单位',		dataIndex: 'organization_name', 	width:200 },
		{ title: '社会统一代码',	dataIndex: 'social_credit_code', 	width:120 },
		{ title: '创建时间', 	dataIndex: 'create_at', 	...$fn.table.time },
		{ title: '状态', 		dataIndex: 'enabled', 		width:80, render:text =>$fn.getState(enabledData,text) },
		{ title: '模板名称',		dataIndex: 'temp_name', 	width:150 },
		TableControls.controls.call(this, { width: 150 })
	]
	
	// 表格操作按钮
	table_controls = (text, { uuid, hosp_name,temp_name }) => [
		TableControls.pushTo.call(this, '编辑',  `edit?uuid=${uuid}&edit=1` ),
		TableControls.pushTo.call(this, '物价管理', `price-control?uuid=${uuid}&hosp_name=${$fn.encode(hosp_name)}&temp_name=${$fn.encode(temp_name)}` ),
	]
	
	// 操作按钮
	controls = () => [
		TableControls.pushTo.call(this, '新增',  `edit?edit=2`, { code:'F1'} ),
		Confirm.call(this,'审核','bs-hospital/exam', { isKeys:true, clear:false, onRefresh:()=>{
            $fn.dataRemove('bs-hospital/select')
			this.getHosptal()
        } }),
		Controls.printer.call(this, { batch:true, code:50101, label:'打印' }),
		// 导出
		Controls.exports.call(this, 'bs-hospital/exportHospitalItemPrice', { code: false, param: { comp_id: $fn.getUser().comp_id } }),
		// 导入
		Controls.imports.call(this, 'bs-hospital/import'),
		// 下载导入模板
		Controls.importTemplate.call(this),
	]
	
	render(){
		return (
			<>
				<Page title='医院客户' controls={this.controls()}>
					{/* 搜索 */}
					{ SearchForm.call(this) }
					{/* 表格 */}
					{ Table.call(this, { isInit: window.isInit }) }
				</Page>
			</>
		)
	}
}

export default Index